package com.fr.report.script.function;

import com.fr.base.core.DateUtils;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.util.Utils;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class FORMAT extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    Object localObject2;
    if (paramArrayOfObject.length < 2)
      return Primitive.ERROR_NAME;
    Object localObject1 = paramArrayOfObject[0];
    String str = paramArrayOfObject[1].toString().trim();
    if ((!(localObject1 instanceof Number)) && (!(localObject1 instanceof Date)))
    {
      localObject2 = localObject1.toString();
      localObject1 = Utils.string2Number((String)localObject2);
      if (localObject1 == null)
        localObject1 = DateUtils.string2Date((String)localObject2, true);
    }
    if (localObject1 instanceof Number)
      return new DecimalFormat(str).format(localObject1);
    if (localObject1 instanceof Date)
    {
      localObject2 = Locale.getDefault();
      if (paramArrayOfObject.length > 2)
      {
        Object localObject3 = paramArrayOfObject[2];
        localObject2 = new Locale(localObject3.toString());
      }
      return new SimpleDateFormat(str, (Locale)localObject2).format(localObject1);
    }
    return localObject1;
  }

  public Function.Type getType()
  {
    return Function.TEXT;
  }

  public String getCN()
  {
    return "FORMAT(object,format) : 返回object的format格式。\nobject 需要被格式化对象，可以是String，数字，Object(常用的有Date, Time)。\nformat 格式化的样式。\n\n示例\nFORMAT(1234.5, \"#,##0.00\") => 1234.50\nFORMAT(1234.5, \"#,##0\") => 1234\n\nFORMAT(1234.5, \"￥#,##0.00\") => ￥1234.50\n\nFORMAT(1.5, \"0%\") => 150%\nFORMAT(1.5, \"0.000%\") => 150.000%\n\nFORMAT(6789, \"##0.0E0\") => 6.789E3\nFORMAT(6789, \"0.00E00\") => 6.79E03\n\nFORMAT(date(2007,1,1), \"EEEEE, MMMMM dd, yyyy\") => 星期一，一月 01，2007\nFORMAT(date(2007,1,13), \"MM/dd/yyyy\") => 01/13/2007\nFORMAT(date(2007,1,13), \"M-d-yy\") => 1-13-07\n\nFORMAT(time(16,23,56), \"h:mm:ss a\") => 4:23:56 下午";
  }

  public String getEN()
  {
    return "FORMAT(object,format) : Return a formatted string of \"object\" according to \"format\".\nobject: The object to format, it could be the type of String, Number or Object.(Date and Time are usually used.)\nformat: The pattern of the format. \n\nExample\nFORMAT(1234.5, \"0.00\") => 1234.50\nFORMAT(1234.5, \"#,##0\") => 1234\n\nFORMAT(1.5, \"0%\") => 150%\nFORMAT(1.5, \"0.000%\") => 150.000%\n\nFORMAT(6789, \"##0.0E0\") => 6.789E3\nFORMAT(6789, \"0.00E00\") => 6.79E03\n\nFORMAT(date(2007,1,13), \"MM/dd/yyyy\") => 01/13/2007\nFORMAT(date(2007,1,13), \"M-d-yy\") => 1-13-07\n\nFORMAT(time(1,23,56), \"HH:mm:ss\") => 01:23:56";
  }
}